package org.example.domain.rebate.service;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.example.domain.rebate.IBehaviorRebateService;
import org.example.domain.rebate.event.SendRebateMessageEvent;
import org.example.domain.rebate.model.arrtrgate.BehaviorRebateAggregate;
import org.example.domain.rebate.model.entity.BehaviorEntity;
import org.example.domain.rebate.model.entity.BehaviorRebateOrderEntity;
import org.example.domain.rebate.model.entity.TaskEntity;
import org.example.domain.rebate.model.valobj.DailyBehaviorRebateVO;
import org.example.domain.rebate.repository.IBehaviorRebateRepository;
import org.example.domain.task.model.volabj.TaskStateVO;
import org.example.types.common.Constants;
import org.example.types.event.BaseEvent;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
@Slf4j
public class BehaviorRebateService implements IBehaviorRebateService {
    @Resource
    private IBehaviorRebateRepository behaviorRebateRepository;
    @Resource
    private SendRebateMessageEvent sendRebateMessageEvent;
    @Override
    public List<String> createOrder(BehaviorEntity behaviorEntity) {
        List<DailyBehaviorRebateVO> dailyBehaviorRebateVOS =  behaviorRebateRepository.queryDailyBehaviorRebate(behaviorEntity.getBehaviorTypeVO());
        List<String> Orders = new ArrayList<>();
        List<BehaviorRebateAggregate> behaviorRebateAggregates = new ArrayList<>();
        for (DailyBehaviorRebateVO dailyBehaviorRebateVO:dailyBehaviorRebateVOS) {
            //拼装业务ID
            String bizId = behaviorEntity.getUserId() + Constants.UNDERLINE + dailyBehaviorRebateVO.getRebateType() + Constants.UNDERLINE + behaviorEntity.getOutBusinessNo();
            //返利订单组装
            BehaviorRebateOrderEntity behaviorRebateOrderEntity = new BehaviorRebateOrderEntity();
             behaviorRebateOrderEntity.setUserId(behaviorEntity.getUserId());
             behaviorRebateOrderEntity.setOrderId(RandomStringUtils.randomNumeric(12));
             behaviorRebateOrderEntity.setBizId(bizId);
             behaviorRebateOrderEntity.setOutBusinessNo(behaviorEntity.getOutBusinessNo());
             behaviorRebateOrderEntity.setBehaviorType(dailyBehaviorRebateVO.getBehaviorType());
             behaviorRebateOrderEntity.setRebateDesc(dailyBehaviorRebateVO.getRebateDesc());
             behaviorRebateOrderEntity.setRebateType(dailyBehaviorRebateVO.getRebateType());
             behaviorRebateOrderEntity.setRebateConfig(dailyBehaviorRebateVO.getRebateConfig());
            Orders.add(behaviorRebateOrderEntity.getOrderId());
             //MQ消息对象
            SendRebateMessageEvent.RebateMessage rebateMessage = SendRebateMessageEvent.RebateMessage.builder()
                    .rebateType(behaviorRebateOrderEntity.getRebateType())
                    .rebateConfig(behaviorRebateOrderEntity.getRebateConfig())
                    .userId(behaviorRebateOrderEntity.getUserId())
                    .bizId(behaviorRebateOrderEntity.getBizId())
                    .build();

            //组装事件消息
            BaseEvent.EventMessage<SendRebateMessageEvent.RebateMessage> rebateMessageEventMessage = sendRebateMessageEvent.buildEventMessage(rebateMessage);
            //组装任务
            TaskEntity taskEntity = TaskEntity.builder()
                    .Message(rebateMessageEventMessage)
                    .MessageId(rebateMessageEventMessage.getId())
                    .state(TaskStateVO.create)
                    .topic(sendRebateMessageEvent.topic())
                    .userId(behaviorEntity.getUserId())
                    .build();

            BehaviorRebateAggregate behaviorRebateAggregate = BehaviorRebateAggregate.builder()
                    .userId(behaviorEntity.getUserId())
                    .behaviorRebateOrderEntity(behaviorRebateOrderEntity)
                    .taskEntity(taskEntity)
                    .build();
            behaviorRebateAggregates.add(behaviorRebateAggregate);
        }
        // 3. 存储聚合对象数据
        behaviorRebateRepository.saveUserRebateRecord(behaviorEntity.getUserId(), behaviorRebateAggregates);
        return Orders;
    }

    @Override
    public List<BehaviorRebateOrderEntity> queryOrderByOutBusinessNo(String userId, String outBusinessNo) {
        return behaviorRebateRepository.queryOrderByOutBusinessNo(userId, outBusinessNo);

    }
}
